<?php
namespace app\site\controller;

use app\common\model\ShopExtendModel;
use app\common\model\ShopGoodsCategoryModel;
use app\common\model\ShopGoodsExtendModel;
use think\Request;

class ShopExtendController extends BaseController
{

    private $extendModel;
    private $goodsExtendModel;
    private $goodsCategoryModel;
    private $goodsCategoryCache;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->extendModel      = new ShopExtendModel();
        $this->goodsExtendModel = new ShopGoodsExtendModel();
        $this->goodsCategoryModel = new ShopGoodsCategoryModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map        = [];
        $query      = [];
        $map['site_id'] = $this->getSiteId();
        $list = $this->extendModel->where($map)->where('extend_status',0)->order('extend_sort asc')->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $data['extend_status'] = 0;
            if(empty($data['cid_path'])){
                $this->error('请选择要绑定的分类！');
            }
            $cidPath = explode(',',$data['cid_path']);
            $cidPath = array_filter($cidPath);
            $cidNames = $this->goodsCategoryModel->where('cid','in',$cidPath)->column('cat_name');
            $data['cid_path'] = implode(',',$cidPath);
            $data['cid_names'] = implode(',',$cidNames);
            $data['cid'] = end($cidPath); //取最后一级
            $result = $this->validate($data, 'ShopExtendValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $info = $this->extendModel->where('cid',$data['cid'])->where('extend_status',1)->find();
            if($info){
                $this->error('该类目下已有对应搜索属性！');
            }
            $this->extendModel->addData($data);
            $this->success('添加成功', url('index'));
        }
        return $this->fetch();
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            if(empty($data['cid_path'])){
                $this->error('请选择要绑定的分类！');
            }
            $cidPath = explode(',',$data['cid_path']);
            $cidPath = array_filter($cidPath);
            $cidNames = $this->goodsCategoryModel->where('cid','in',$cidPath)->column('cat_name');
            $data['cid_path'] = implode(',',$cidPath);
            $data['cid_names'] = implode(',',$cidNames);
            $data['cid'] = end($cidPath); //取最后一级
            $result = $this->validate($data, 'ShopExtendValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $info = $this->extendModel->where('cid',$data['cid'])->where('extend_status',1)->find();
            if($info){
                $this->error('该类目下已有对应搜索属性！');
            }
            $this->extendModel->editData($data);
            $this->success('操作成功！', url('index'));
        }
        $extendId = $this->request->param('extend_id');
        $info     = $this->extendModel->where('extend_id', $extendId)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $extendId = $this->request->param('extend_id/d');
            $this->extendModel->where('extend_id', $extendId)->delete();
            $this->success('删除成功！');
        }
    }

}
